<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>

<title>Ruby on Rails 3.1 Release Notes — Ruby on Rails Guides</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />

<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />

<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />

<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
</head>
<body class="guide">
  <div id="topNav">
    <div class="wrapper">
      <strong class="more-info-label">更多内容 <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
      <span class="red-button more-info-button">
        更多内容
      </span>
      <ul class="more-info-links s-hidden">
        <li class="more-info"><a href="http://weblog.rubyonrails.org/">博客</a></li>
        <li class="more-info"><a href="http://guides.rubyonrails.org/">指南</a></li>
        <li class="more-info"><a href="http://api.rubyonrails.org/">API</a></li>
        <li class="more-info"><a href="http://stackoverflow.com/questions/tagged/ruby-on-rails">提问</a></li>
        <li class="more-info"><a href="https://github.com/rails/rails">到 GitHub 贡献</a></li>
        <li class="more-info"><a href="https://ruby-china.org/">Ruby China 社区</a></li>
      </ul>
    </div>
  </div>
  <div id="header">
    <div class="wrapper clearfix">
      <h1><a href="index.html" title="返回首页">Rails 指南</a></h1>
      <ul class="nav">
        <li><a class="nav-item" href="index.html">首页</a></li>
        <li class="guides-index guides-index-large">
          <a href="index.html" id="guidesMenu" class="guides-index-item nav-item">指南索引</a>
          <div id="guides" class="clearfix" style="display: none;">
            <hr />
              <dl class="L">
                <dt>新手入门</dt>
                <dd><a href="getting_started.html">Rails 入门</a></dd>
                <dt>模型</dt>
                <dd><a href="active_record_basics.html">Active Record 基础</a></dd>
                <dd><a href="active_record_migrations.html">Active Record 迁移</a></dd>
                <dd><a href="active_record_validations.html">Active Record 数据验证</a></dd>
                <dd><a href="active_record_callbacks.html">Active Record 回调</a></dd>
                <dd><a href="association_basics.html">Active Record 关联</a></dd>
                <dd><a href="active_record_querying.html">Active Record 查询接口</a></dd>
                <dt>视图</dt>
                <dd><a href="layouts_and_rendering.html">Rails 布局和视图渲染</a></dd>
                <dd><a href="form_helpers.html">Action View 表单辅助方法</a></dd>
                <dt>控制器</dt>
                <dd><a href="action_controller_overview.html">Action Controller 概览</a></dd>
                <dd><a href="routing.html">Rails 路由全解</a></dd>
              </dl>
              <dl class="R">
                <dt>深入探索</dt>
                <dd><a href="active_support_core_extensions.html">Active Support 核心扩展</a></dd>
                <dd><a href="i18n.html">Rails 国际化 API</a></dd>
                <dd><a href="action_mailer_basics.html">Action Mailer 基础</a></dd>
                <dd><a href="active_job_basics.html">Active Job 基础</a></dd>
                <dd><a href="testing.html">Rails 应用测试指南</a></dd>
                <dd><a href="security.html">Ruby on Rails 安全指南</a></dd>
                <dd><a href="debugging_rails_applications.html">调试 Rails 应用</a></dd>
                <dd><a href="configuring.html">配置 Rails 应用</a></dd>
                <dd><a href="command_line.html">Rails 命令行</a></dd>
                <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
                <dd><a href="working_with_javascript_in_rails.html">在 Rails 中使用 JavaScript</a></dd>
                <dd><a href="autoloading_and_reloading_constants.html">自动加载和重新加载常量</a></dd>
                <dd><a href="caching_with_rails.html">Rails 缓存概览</a></dd>
                <dd><a href="api_app.html">使用 Rails 开发只提供 API 的应用</a></dd>
                <dd><a href="action_cable_overview.html">Action Cable 概览</a></dd>
                <dt>扩展 Rails</dt>
                <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
                <dd><a href="generators.html">创建及定制 Rails 生成器和模板</a></dd>
                <dt>为 Ruby on Rails 做贡献</dt>
                <dd><a href="contributing_to_ruby_on_rails.html">为 Ruby on Rails 做贡献</a></dd>
                <dd><a href="api_documentation_guidelines.html">API 文档指导方针</a></dd>
                <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南指导方针</a></dd>
                <dt>维护方针</dt>
                <dd><a href="maintenance_policy.html">Ruby on Rails 的维护方针</a></dd>
                <dt>发布记</dt>
                <dd><a href="upgrading_ruby_on_rails.html">Ruby on Rails 升级指南</a></dd>
                <dd><a href="5_0_release_notes.html">Ruby on Rails 5.0 发布记</a></dd>
                <dd><a href="4_2_release_notes.html">Ruby on Rails 4.2 发布记</a></dd>
                <dd><a href="4_1_release_notes.html">Ruby on Rails 4.1 发布记</a></dd>
                <dd><a href="4_0_release_notes.html">Ruby on Rails 4.0 发布记</a></dd>
                <dd><a href="3_2_release_notes.html">Ruby on Rails 3.2 发布记</a></dd>
                <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 发布记</a></dd>
                <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 发布记</a></dd>
                <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 发布记</a></dd>
                <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 发布记</a></dd>
              </dl>
          </div>
        </li>
        <li><a class="nav-item" href="contributing_to_ruby_on_rails.html">贡献</a></li>
        <li><a class="nav-item" href="credits.html">感谢</a></li>
        <li class="guides-index guides-index-small">
          <select class="guides-index-item nav-item">
            <option value="index.html">指南索引</option>
              <optgroup label="新手入门">
                  <option value="getting_started.html">Rails 入门</option>
              </optgroup>
              <optgroup label="模型">
                  <option value="active_record_basics.html">Active Record 基础</option>
                  <option value="active_record_migrations.html">Active Record 迁移</option>
                  <option value="active_record_validations.html">Active Record 数据验证</option>
                  <option value="active_record_callbacks.html">Active Record 回调</option>
                  <option value="association_basics.html">Active Record 关联</option>
                  <option value="active_record_querying.html">Active Record 查询接口</option>
              </optgroup>
              <optgroup label="视图">
                  <option value="layouts_and_rendering.html">Rails 布局和视图渲染</option>
                  <option value="form_helpers.html">Action View 表单辅助方法</option>
              </optgroup>
              <optgroup label="控制器">
                  <option value="action_controller_overview.html">Action Controller 概览</option>
                  <option value="routing.html">Rails 路由全解</option>
              </optgroup>
              <optgroup label="深入探索">
                  <option value="active_support_core_extensions.html">Active Support 核心扩展</option>
                  <option value="i18n.html">Rails 国际化 API</option>
                  <option value="action_mailer_basics.html">Action Mailer 基础</option>
                  <option value="active_job_basics.html">Active Job 基础</option>
                  <option value="testing.html">Rails 应用测试指南</option>
                  <option value="security.html">Ruby on Rails 安全指南</option>
                  <option value="debugging_rails_applications.html">调试 Rails 应用</option>
                  <option value="configuring.html">配置 Rails 应用</option>
                  <option value="command_line.html">Rails 命令行</option>
                  <option value="asset_pipeline.html">Asset Pipeline</option>
                  <option value="working_with_javascript_in_rails.html">在 Rails 中使用 JavaScript</option>
                  <option value="autoloading_and_reloading_constants.html">自动加载和重新加载常量</option>
                  <option value="caching_with_rails.html">Rails 缓存概览</option>
                  <option value="api_app.html">使用 Rails 开发只提供 API 的应用</option>
                  <option value="action_cable_overview.html">Action Cable 概览</option>
              </optgroup>
              <optgroup label="扩展 Rails">
                  <option value="rails_on_rack.html">Rails on Rack</option>
                  <option value="generators.html">创建及定制 Rails 生成器和模板</option>
              </optgroup>
              <optgroup label="为 Ruby on Rails 做贡献">
                  <option value="contributing_to_ruby_on_rails.html">为 Ruby on Rails 做贡献</option>
                  <option value="api_documentation_guidelines.html">API 文档指导方针</option>
                  <option value="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南指导方针</option>
              </optgroup>
              <optgroup label="维护方针">
                  <option value="maintenance_policy.html">Ruby on Rails 的维护方针</option>
              </optgroup>
              <optgroup label="发布记">
                  <option value="upgrading_ruby_on_rails.html">Ruby on Rails 升级指南</option>
                  <option value="5_0_release_notes.html">Ruby on Rails 5.0 发布记</option>
                  <option value="4_2_release_notes.html">Ruby on Rails 4.2 发布记</option>
                  <option value="4_1_release_notes.html">Ruby on Rails 4.1 发布记</option>
                  <option value="4_0_release_notes.html">Ruby on Rails 4.0 发布记</option>
                  <option value="3_2_release_notes.html">Ruby on Rails 3.2 发布记</option>
                  <option value="3_1_release_notes.html">Ruby on Rails 3.1 发布记</option>
                  <option value="3_0_release_notes.html">Ruby on Rails 3.0 发布记</option>
                  <option value="2_3_release_notes.html">Ruby on Rails 2.3 发布记</option>
                  <option value="2_2_release_notes.html">Ruby on Rails 2.2 发布记</option>
              </optgroup>
          </select>
        </li>
      </ul>
    </div>
  </div>
  <hr class="hide" />

  <div id="feature">
    <div class="wrapper">
      <h2>Ruby on Rails 3.1 Release Notes</h2><p>Highlights in Rails 3.1:</p>
<ul>
<li>Streaming</li>
<li>Reversible Migrations</li>
<li>Assets Pipeline</li>
<li>jQuery as the default JavaScript library</li>
</ul>
<p>These release notes cover only the major changes. To learn about various bug
fixes and changes, please refer to the change logs or check out the <a href="https://github.com/rails/rails/commits/3-1-stable">list of
commits</a> in the main Rails
repository on GitHub.</p>

              <div id="subCol">
          <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />目录</h3>
          <ol class="chapters">
<li>
<a href="#upgrading-to-rails-3-1">Upgrading to Rails 3.1</a>

<ul>
<li><a href="#rails-3-1-requires-at-least-ruby-1-8-7">Rails 3.1 requires at least Ruby 1.8.7</a></li>
<li><a href="#what-to-update-in-your-apps">What to update in your apps</a></li>
</ul>
</li>
<li>
<a href="#creating-a-rails-3-1-application">Creating a Rails 3.1 application</a>

<ul>
<li><a href="#vendoring-gems">Vendoring Gems</a></li>
<li><a href="#living-on-the-edge">Living on the Edge</a></li>
</ul>
</li>
<li>
<a href="#rails-architectural-changes">Rails Architectural Changes</a>

<ul>
<li><a href="#assets-pipeline">Assets Pipeline</a></li>
<li><a href="#http-streaming">HTTP Streaming</a></li>
<li><a href="#default-js-library-is-now-jquery">Default JS library is now jQuery</a></li>
<li><a href="#identity-map">Identity Map</a></li>
</ul>
</li>
<li><a href="#railties">Railties</a></li>
<li>
<a href="#action-pack">Action Pack</a>

<ul>
<li><a href="#action-controller">Action Controller</a></li>
<li><a href="#action-dispatch">Action Dispatch</a></li>
<li><a href="#action-view">Action View</a></li>
</ul>
</li>
<li><a href="#active-record">Active Record</a></li>
<li><a href="#active-model">Active Model</a></li>
<li><a href="#active-resource">Active Resource</a></li>
<li><a href="#active-support">Active Support</a></li>
<li><a href="#credits">Credits</a></li>
</ol>

        </div>

    </div>
  </div>

  <div id="container">
    <div class="wrapper">
      <div id="mainCol">
        <h3 id="upgrading-to-rails-3-1">1 Upgrading to Rails 3.1</h3><p>If you're upgrading an existing application, it's a great idea to have good test coverage before going in. You should also first upgrade to Rails 3 in case you haven't and make sure your application still runs as expected before attempting to update to Rails 3.1. Then take heed of the following changes:</p><h4 id="rails-3-1-requires-at-least-ruby-1-8-7">1.1 Rails 3.1 requires at least Ruby 1.8.7</h4><p>Rails 3.1 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been dropped officially and you should upgrade as early as possible. Rails 3.1 is also compatible with Ruby 1.9.2.</p><div class="info"><p>Note that Ruby 1.8.7 p248 and p249 have marshaling bugs that crash Rails. Ruby Enterprise Edition have these fixed since release 1.8.7-2010.02 though. On the 1.9 front, Ruby 1.9.1 is not usable because it outright segfaults, so if you want to use 1.9.x jump on 1.9.2 for smooth sailing.</p></div><h4 id="what-to-update-in-your-apps">1.2 What to update in your apps</h4><p>The following changes are meant for upgrading your application to Rails 3.1.3, the latest 3.1.x version of Rails.</p><h5 id="gemfile">1.2.1 Gemfile</h5><p>Make the following changes to your <code>Gemfile</code>.</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
gem 'rails', '= 3.1.3'
gem 'mysql2'

# Needed for the new asset pipeline
group :assets do
  gem 'sass-rails',   "~&gt; 3.1.5"
  gem 'coffee-rails', "~&gt; 3.1.1"
  gem 'uglifier',     "&gt;= 1.0.3"
end

# jQuery is the default JavaScript library in Rails 3.1
gem 'jquery-rails'

</pre>
</div>
<h5 id="config-application-rb">1.2.2 config/application.rb</h5>
<ul>
<li>
<p>The asset pipeline requires the following additions:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.assets.enabled = true
config.assets.version = '1.0'

</pre>
</div>
</li>
<li>
<p>If your application is using the "/assets" route for a resource you may want change the prefix used for assets to avoid conflicts:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Defaults to '/assets'
config.assets.prefix = '/asset-files'

</pre>
</div>
</li>
</ul>
<h5 id="config-environments-development-rb">1.2.3 config/environments/development.rb</h5>
<ul>
<li><p>Remove the RJS setting <code>config.action_view.debug_rjs = true</code>.</p></li>
<li>
<p>Add the following, if you enable the asset pipeline.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Do not compress assets
config.assets.compress = false

# Expands the lines which load the assets
config.assets.debug = true

</pre>
</div>
</li>
</ul>
<h5 id="config-environments-production-rb">1.2.4 config/environments/production.rb</h5>
<ul>
<li>
<p>Again, most of the changes below are for the asset pipeline. You can read more about these in the <a href="asset_pipeline.html">Asset Pipeline</a> guide.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Compress JavaScripts and CSS
config.assets.compress = true

# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false

# Generate digests for assets URLs
config.assets.digest = true

# Defaults to Rails.root.join("public/assets")
# config.assets.manifest = YOUR_PATH

# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile `= %w( search.js )

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true

</pre>
</div>
</li>
</ul>
<h5 id="config-environments-test-rb">1.2.5 config/environments/test.rb</h5><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Configure static asset server for tests with Cache-Control for performance
config.serve_static_assets = true
config.static_cache_control = "public, max-age=3600"

</pre>
</div>
<h5 id="config-initializers-wrap-parameters-rb">1.2.6 config/initializers/wrap_parameters.rb</h5>
<ul>
<li>
<p>Add this file with the following contents, if you wish to wrap parameters into a nested hash. This is on by default in new applications.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Be sure to restart your server when you modify this file.
# This file contains settings for ActionController::ParamsWrapper which
# is enabled by default.

# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
ActiveSupport.on_load(:action_controller) do
  wrap_parameters :format =&gt; [:json]
end

# Disable root element in JSON by default.
ActiveSupport.on_load(:active_record) do
  self.include_root_in_json = false
end

</pre>
</div>
</li>
</ul>
<h5 id="remove-cache-and-concat-options-in-asset-helpers-references-in-views">1.2.7 Remove :cache and :concat options in asset helpers references in views</h5>
<ul>
<li>With the Asset Pipeline the :cache and :concat options aren't used anymore, delete these options from your views.</li>
</ul>
<h3 id="creating-a-rails-3-1-application">2 Creating a Rails 3.1 application</h3><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
# You should have the 'rails' RubyGem installed
$ rails new myapp
$ cd myapp

</pre>
</div>
<h4 id="vendoring-gems">2.1 Vendoring Gems</h4><p>Rails now uses a <code>Gemfile</code> in the application root to determine the gems you require for your application to start. This <code>Gemfile</code> is processed by the <a href="https://github.com/carlhuda/bundler">Bundler</a> gem, which then installs all your dependencies. It can even install all the dependencies locally to your application so that it doesn't depend on the system gems.</p><p>More information: - <a href="http://bundler.io/">bundler homepage</a></p><h4 id="living-on-the-edge">2.2 Living on the Edge</h4><p><code>Bundler</code> and <code>Gemfile</code> makes freezing your Rails application easy as pie with the new dedicated <code>bundle</code> command. If you want to bundle straight from the Git repository, you can pass the <code>--edge</code> flag:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails new myapp --edge

</pre>
</div>
<p>If you have a local checkout of the Rails repository and want to generate an application using that, you can pass the <code>--dev</code> flag:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ ruby /path/to/rails/railties/bin/rails new myapp --dev

</pre>
</div>
<h3 id="rails-architectural-changes">3 Rails Architectural Changes</h3><h4 id="assets-pipeline">3.1 Assets Pipeline</h4><p>The major change in Rails 3.1 is the Assets Pipeline. It makes CSS and JavaScript first-class code citizens and enables proper organization, including use in plugins and engines.</p><p>The assets pipeline is powered by <a href="https://github.com/rails/sprockets">Sprockets</a> and is covered in the <a href="asset_pipeline.html">Asset Pipeline</a> guide.</p><h4 id="http-streaming">3.2 HTTP Streaming</h4><p>HTTP Streaming is another change that is new in Rails 3.1. This lets the browser download your stylesheets and JavaScript files while the server is still generating the response. This requires Ruby 1.9.2, is opt-in and requires support from the web server as well, but the popular combo of NGINX and Unicorn is ready to take advantage of it.</p><h4 id="default-js-library-is-now-jquery">3.3 Default JS library is now jQuery</h4><p>jQuery is the default JavaScript library that ships with Rails 3.1. But if you use Prototype, it's simple to switch.</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails new myapp -j prototype

</pre>
</div>
<h4 id="identity-map">3.4 Identity Map</h4><p>Active Record has an Identity Map in Rails 3.1. An identity map keeps previously instantiated records and returns the object associated with the record if accessed again. The identity map is created on a per-request basis and is flushed at request completion.</p><p>Rails 3.1 comes with the identity map turned off by default.</p><h3 id="railties">4 Railties</h3>
<ul>
<li><p>jQuery is the new default JavaScript library.</p></li>
<li><p>jQuery and Prototype are no longer vendored and is provided from now on by the <code>jquery-rails</code> and <code>prototype-rails</code> gems.</p></li>
<li><p>The application generator accepts an option <code>-j</code> which can be an arbitrary string. If passed "foo", the gem "foo-rails" is added to the <code>Gemfile</code>, and the application JavaScript manifest requires "foo" and "foo_ujs". Currently only "prototype-rails" and "jquery-rails" exist and provide those files via the asset pipeline.</p></li>
<li><p>Generating an application or a plugin runs <code>bundle install</code> unless <code>--skip-gemfile</code> or <code>--skip-bundle</code> is specified.</p></li>
<li><p>The controller and resource generators will now automatically produce asset stubs (this can be turned off with <code>--skip-assets</code>). These stubs will use CoffeeScript and Sass, if those libraries are available.</p></li>
<li><p>Scaffold and app generators use the Ruby 1.9 style hash when running on Ruby 1.9. To generate old style hash, <code>--old-style-hash</code> can be passed.</p></li>
<li><p>Scaffold controller generator creates format block for JSON instead of XML.</p></li>
<li><p>Active Record logging is directed to STDOUT and shown inline in the console.</p></li>
<li><p>Added <code>config.force_ssl</code> configuration which loads <code>Rack::SSL</code> middleware and force all requests to be under HTTPS protocol.</p></li>
<li><p>Added <code>rails plugin new</code> command which generates a Rails plugin with gemspec, tests and a dummy application for testing.</p></li>
<li><p>Added <code>Rack::Etag</code> and <code>Rack::ConditionalGet</code> to the default middleware stack.</p></li>
<li><p>Added <code>Rack::Cache</code> to the default middleware stack.</p></li>
<li><p>Engines received a major update - You can mount them at any path, enable assets, run generators etc.</p></li>
</ul>
<h3 id="action-pack">5 Action Pack</h3><h4 id="action-controller">5.1 Action Controller</h4>
<ul>
<li><p>A warning is given out if the CSRF token authenticity cannot be verified.</p></li>
<li><p>Specify <code>force_ssl</code> in a controller to force the browser to transfer data via HTTPS protocol on that particular controller. To limit to specific actions, <code>:only</code> or <code>:except</code> can be used.</p></li>
<li><p>Sensitive query string parameters specified in <code>config.filter_parameters</code> will now be filtered out from the request paths in the log.</p></li>
<li><p>URL parameters which return <code>nil</code> for <code>to_param</code> are now removed from the query string.</p></li>
<li><p>Added <code>ActionController::ParamsWrapper</code> to wrap parameters into a nested hash, and will be turned on for JSON request in new applications by default. This can be customized in <code>config/initializers/wrap_parameters.rb</code>.</p></li>
<li><p>Added <code>config.action_controller.include_all_helpers</code>. By default <code>helper :all</code> is done in <code>ActionController::Base</code>, which includes all the helpers by default. Setting <code>include_all_helpers</code> to <code>false</code> will result in including only application_helper and the helper corresponding to controller (like foo_helper for foo_controller).</p></li>
<li><p><code>url_for</code> and named url helpers now accept <code>:subdomain</code> and <code>:domain</code> as options.</p></li>
<li>
<p>Added <code>Base.http_basic_authenticate_with</code> to do simple http basic authentication with a single class method call.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class PostsController &lt; ApplicationController
  USER_NAME, PASSWORD = "dhh", "secret"

  before_filter :authenticate, :except =&gt; [ :index ]

  def index
    render :text =&gt; "Everyone can see me!"
  end

  def edit
    render :text =&gt; "I'm only accessible if you know the password"
  end

  private
    def authenticate
      authenticate_or_request_with_http_basic do |user_name, password|
        user_name == USER_NAME &amp;&amp; password == PASSWORD
      end
    end
end

</pre>
</div>
<p>..can now be written as</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class PostsController &lt; ApplicationController
  http_basic_authenticate_with :name =&gt; "dhh", :password =&gt; "secret", :except =&gt; :index

  def index
    render :text =&gt; "Everyone can see me!"
  end

  def edit
    render :text =&gt; "I'm only accessible if you know the password"
  end
end

</pre>
</div>
</li>
<li>
<p>Added streaming support, you can enable it with:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class PostsController &lt; ActionController::Base
  stream
end

</pre>
</div>
<p>You can restrict it to some actions by using <code>:only</code> or <code>:except</code>. Please read the docs at <a href="http://api.rubyonrails.org/v3.1.0/classes/ActionController/Streaming.html"><code>ActionController::Streaming</code></a> for more information.</p>
</li>
<li><p>The redirect route method now also accepts a hash of options which will only change the parts of the url in question, or an object which responds to call, allowing for redirects to be reused.</p></li>
</ul>
<h4 id="action-dispatch">5.2 Action Dispatch</h4>
<ul>
<li><p><code>config.action_dispatch.x_sendfile_header</code> now defaults to <code>nil</code> and <code>config/environments/production.rb</code> doesn't set any particular value for it. This allows servers to set it through <code>X-Sendfile-Type</code>.</p></li>
<li><p><code>ActionDispatch::MiddlewareStack</code> now uses composition over inheritance and is no longer an array.</p></li>
<li><p>Added <code>ActionDispatch::Request.ignore_accept_header</code> to ignore accept headers.</p></li>
<li><p>Added <code>Rack::Cache</code> to the default stack.</p></li>
<li><p>Moved etag responsibility from <code>ActionDispatch::Response</code> to the middleware stack.</p></li>
<li><p>Rely on <code>Rack::Session</code> stores API for more compatibility across the Ruby world. This is backwards incompatible since <code>Rack::Session</code> expects <code>#get_session</code> to accept four arguments and requires <code>#destroy_session</code> instead of simply <code>#destroy</code>.</p></li>
<li><p>Template lookup now searches further up in the inheritance chain.</p></li>
</ul>
<h4 id="action-view">5.3 Action View</h4>
<ul>
<li><p>Added an <code>:authenticity_token</code> option to <code>form_tag</code> for custom handling or to omit the token by passing <code>:authenticity_token =&gt; false</code>.</p></li>
<li><p>Created <code>ActionView::Renderer</code> and specified an API for <code>ActionView::Context</code>.</p></li>
<li><p>In place <code>SafeBuffer</code> mutation is prohibited in Rails 3.1.</p></li>
<li><p>Added HTML5 <code>button_tag</code> helper.</p></li>
<li><p><code>file_field</code> automatically adds <code>:multipart =&gt; true</code> to the enclosing form.</p></li>
<li>
<p>Added a convenience idiom to generate HTML5 data-* attributes in tag helpers from a <code>:data</code> hash of options:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
tag("div", :data =&gt; {:name =&gt; 'Stephen', :city_state =&gt; %w(Chicago IL)})
# =&gt; &lt;div data-name="Stephen" data-city-state="[&amp;quot;Chicago&amp;quot;,&amp;quot;IL&amp;quot;]" /&gt;

</pre>
</div>
</li>
</ul>
<p>Keys are dasherized. Values are JSON-encoded, except for strings and symbols.</p>
<ul>
<li><p><code>csrf_meta_tag</code> is renamed to <code>csrf_meta_tags</code> and aliases <code>csrf_meta_tag</code> for backwards compatibility.</p></li>
<li><p>The old template handler API is deprecated and the new API simply requires a template handler to respond to call.</p></li>
<li><p>rhtml and rxml are finally removed as template handlers.</p></li>
<li><p><code>config.action_view.cache_template_loading</code> is brought back which allows to decide whether templates should be cached or not.</p></li>
<li><p>The submit form helper does not generate an id "object_name_id" anymore.</p></li>
<li><p>Allows <code>FormHelper#form_for</code> to specify the <code>:method</code> as a direct option instead of through the <code>:html</code> hash. <code>form_for(@post, remote: true, method: :delete)</code> instead of <code>form_for(@post, remote: true, html: { method: :delete })</code>.</p></li>
<li><p>Provided <code>JavaScriptHelper#j()</code> as an alias for <code>JavaScriptHelper#escape_javascript()</code>. This supersedes the <code>Object#j()</code> method that the JSON gem adds within templates using the JavaScriptHelper.</p></li>
<li><p>Allows AM/PM format in datetime selectors.</p></li>
<li><p><code>auto_link</code> has been removed from Rails and extracted into the <a href="https://github.com/tenderlove/rails_autolink">rails_autolink gem</a></p></li>
</ul>
<h3 id="active-record">6 Active Record</h3>
<ul>
<li>
<p>Added a class method <code>pluralize_table_names</code> to singularize/pluralize table names of individual models. Previously this could only be set globally for all models through <code>ActiveRecord::Base.pluralize_table_names</code>.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class User &lt; ActiveRecord::Base
  self.pluralize_table_names = false
end

</pre>
</div>
</li>
<li>
<p>Added block setting of attributes to singular associations. The block will get called after the instance is initialized.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class User &lt; ActiveRecord::Base
  has_one :account
end

user.build_account{ |a| a.credit_limit = 100.0 }

</pre>
</div>
</li>
<li><p>Added <code>ActiveRecord::Base.attribute_names</code> to return a list of attribute names. This will return an empty array if the model is abstract or the table does not exist.</p></li>
<li><p>CSV Fixtures are deprecated and support will be removed in Rails 3.2.0.</p></li>
<li>
<p><code>ActiveRecord#new</code>, <code>ActiveRecord#create</code> and <code>ActiveRecord#update_attributes</code> all accept a second hash as an option that allows you to specify which role to consider when assigning attributes. This is built on top of Active Model's new mass assignment capabilities:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class Post &lt; ActiveRecord::Base
  attr_accessible :title
  attr_accessible :title, :published_at, :as =&gt; :admin
end

Post.new(params[:post], :as =&gt; :admin)

</pre>
</div>
</li>
<li><p><code>default_scope</code> can now take a block, lambda, or any other object which responds to call for lazy evaluation.</p></li>
<li><p>Default scopes are now evaluated at the latest possible moment, to avoid problems where scopes would be created which would implicitly contain the default scope, which would then be impossible to get rid of via Model.unscoped.</p></li>
<li><p>PostgreSQL adapter only supports PostgreSQL version 8.2 and higher.</p></li>
<li><p><code>ConnectionManagement</code> middleware is changed to clean up the connection pool after the rack body has been flushed.</p></li>
<li><p>Added an <code>update_column</code> method on Active Record. This new method updates a given attribute on an object, skipping validations and callbacks. It is recommended to use <code>update_attributes</code> or <code>update_attribute</code> unless you are sure you do not want to execute any callback, including the modification of the <code>updated_at</code> column. It should not be called on new records.</p></li>
<li><p>Associations with a <code>:through</code> option can now use any association as the through or source association, including other associations which have a <code>:through</code> option and <code>has_and_belongs_to_many</code> associations.</p></li>
<li><p>The configuration for the current database connection is now accessible via <code>ActiveRecord::Base.connection_config</code>.</p></li>
<li>
<p>limits and offsets are removed from COUNT queries unless both are supplied.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
People.limit(1).count           # =&gt; 'SELECT COUNT(*) FROM people'
People.offset(1).count          # =&gt; 'SELECT COUNT(*) FROM people'
People.limit(1).offset(1).count # =&gt; 'SELECT COUNT(*) FROM people LIMIT 1 OFFSET 1'

</pre>
</div>
</li>
<li><p><code>ActiveRecord::Associations::AssociationProxy</code> has been split. There is now an <code>Association</code> class (and subclasses) which are responsible for operating on associations, and then a separate, thin wrapper called <code>CollectionProxy</code>, which proxies collection associations. This prevents namespace pollution, separates concerns, and will allow further refactorings.</p></li>
<li><p>Singular associations (<code>has_one</code>, <code>belongs_to</code>) no longer have a proxy and simply returns the associated record or <code>nil</code>. This means that you should not use undocumented methods such as <code>bob.mother.create</code> - use <code>bob.create_mother</code> instead.</p></li>
<li><p>Support the <code>:dependent</code> option on <code>has_many :through</code> associations. For historical and practical reasons, <code>:delete_all</code> is the default deletion strategy employed by <code>association.delete(*records)</code>, despite the fact that the default strategy is <code>:nullify</code> for regular has_many. Also, this only works at all if the source reflection is a belongs_to. For other situations, you should directly modify the through association.</p></li>
<li><p>The behavior of <code>association.destroy</code> for <code>has_and_belongs_to_many</code> and <code>has_many :through</code> is changed. From now on, 'destroy' or 'delete' on an association will be taken to mean 'get rid of the link', not (necessarily) 'get rid of the associated records'.</p></li>
<li><p>Previously, <code>has_and_belongs_to_many.destroy(*records)</code> would destroy the records themselves. It would not delete any records in the join table. Now, it deletes the records in the join table.</p></li>
<li><p>Previously, <code>has_many_through.destroy(*records)</code> would destroy the records themselves, and the records in the join table. [Note: This has not always been the case; previous version of Rails only deleted the records themselves.] Now, it destroys only the records in the join table.</p></li>
<li><p>Note that this change is backwards-incompatible to an extent, but there is unfortunately no way to 'deprecate' it before changing it. The change is being made in order to have consistency as to the meaning of 'destroy' or 'delete' across the different types of associations. If you wish to destroy the records themselves, you can do <code>records.association.each(&amp;:destroy)</code>.</p></li>
<li>
<p>Add <code>:bulk =&gt; true</code> option to <code>change_table</code> to make all the schema changes defined in a block using a single ALTER statement.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
change_table(:users, :bulk =&gt; true) do |t|
  t.string :company_name
  t.change :birthdate, :datetime
end

</pre>
</div>
</li>
<li><p>Removed support for accessing attributes on a <code>has_and_belongs_to_many</code> join table. <code>has_many :through</code> needs to be used.</p></li>
<li><p>Added a <code>create_association!</code> method for <code>has_one</code> and <code>belongs_to</code> associations.</p></li>
<li>
<p>Migrations are now reversible, meaning that Rails will figure out how to reverse your migrations. To use reversible migrations, just define the <code>change</code> method.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class MyMigration &lt; ActiveRecord::Migration
  def change
    create_table(:horses) do |t|
      t.column :content, :text
      t.column :remind_at, :datetime
    end
  end
end

</pre>
</div>
</li>
<li><p>Some things cannot be automatically reversed for you. If you know how to reverse those things, you should define <code>up</code> and <code>down</code> in your migration. If you define something in change that cannot be reversed, an <code>IrreversibleMigration</code> exception will be raised when going down.</p></li>
<li>
<p>Migrations now use instance methods rather than class methods:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class FooMigration &lt; ActiveRecord::Migration
  def up # Not self.up
    ...
  end
end

</pre>
</div>
</li>
<li><p>Migration files generated from model and constructive migration generators (for example, add_name_to_users) use the reversible migration's <code>change</code> method instead of the ordinary <code>up</code> and <code>down</code> methods.</p></li>
<li>
<p>Removed support for interpolating string SQL conditions on associations. Instead, a proc should be used.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
has_many :things, :conditions =&gt; 'foo = #{bar}'          # before
has_many :things, :conditions =&gt; proc { "foo = #{bar}" } # after

</pre>
</div>
<p>Inside the proc, <code>self</code> is the object which is the owner of the association, unless you are eager loading the association, in which case <code>self</code> is the class which the association is within.</p>
<p>You can have any "normal" conditions inside the proc, so the following will work too:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
has_many :things, :conditions =&gt; proc { ["foo = ?", bar] }

</pre>
</div>
</li>
<li><p>Previously <code>:insert_sql</code> and <code>:delete_sql</code> on <code>has_and_belongs_to_many</code> association allowed you to call 'record' to get the record being inserted or deleted. This is now passed as an argument to the proc.</p></li>
<li>
<p>Added <code>ActiveRecord::Base#has_secure_password</code> (via <code>ActiveModel::SecurePassword</code>) to encapsulate dead-simple password usage with BCrypt encryption and salting.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Schema: User(name:string, password_digest:string, password_salt:string)
class User &lt; ActiveRecord::Base
  has_secure_password
end

</pre>
</div>
</li>
<li><p>When a model is generated <code>add_index</code> is added by default for <code>belongs_to</code> or <code>references</code> columns.</p></li>
<li><p>Setting the id of a <code>belongs_to</code> object will update the reference to the object.</p></li>
<li><p><code>ActiveRecord::Base#dup</code> and <code>ActiveRecord::Base#clone</code> semantics have changed to closer match normal Ruby dup and clone semantics.</p></li>
<li><p>Calling <code>ActiveRecord::Base#clone</code> will result in a shallow copy of the record, including copying the frozen state. No callbacks will be called.</p></li>
<li><p>Calling <code>ActiveRecord::Base#dup</code> will duplicate the record, including calling after initialize hooks. Frozen state will not be copied, and all associations will be cleared. A duped record will return <code>true</code> for <code>new_record?</code>, have a <code>nil</code> id field, and is saveable.</p></li>
<li><p>The query cache now works with prepared statements. No changes in the applications are required.</p></li>
</ul>
<h3 id="active-model">7 Active Model</h3>
<ul>
<li><p><code>attr_accessible</code> accepts an option <code>:as</code> to specify a role.</p></li>
<li><p><code>InclusionValidator</code>, <code>ExclusionValidator</code>, and <code>FormatValidator</code> now accepts an option which can be a proc, a lambda, or anything that respond to <code>call</code>. This option will be called with the current record as an argument and returns an object which respond to <code>include?</code> for <code>InclusionValidator</code> and <code>ExclusionValidator</code>, and returns a regular expression object for <code>FormatValidator</code>.</p></li>
<li><p>Added <code>ActiveModel::SecurePassword</code> to encapsulate dead-simple password usage with BCrypt encryption and salting.</p></li>
<li><p><code>ActiveModel::AttributeMethods</code> allows attributes to be defined on demand.</p></li>
<li><p>Added support for selectively enabling and disabling observers.</p></li>
<li><p>Alternate <code>I18n</code> namespace lookup is no longer supported.</p></li>
</ul>
<h3 id="active-resource">8 Active Resource</h3>
<ul>
<li>
<p>The default format has been changed to JSON for all requests. If you want to continue to use XML you will need to set <code>self.format = :xml</code> in the class. For example,</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class User &lt; ActiveResource::Base
  self.format = :xml
end

</pre>
</div>
</li>
</ul>
<h3 id="active-support">9 Active Support</h3>
<ul>
<li><p><code>ActiveSupport::Dependencies</code> now raises <code>NameError</code> if it finds an existing constant in <code>load_missing_constant</code>.</p></li>
<li><p>Added a new reporting method <code>Kernel#quietly</code> which silences both <code>STDOUT</code> and <code>STDERR</code>.</p></li>
<li><p>Added <code>String#inquiry</code> as a convenience method for turning a String into a <code>StringInquirer</code> object.</p></li>
<li><p>Added <code>Object#in?</code> to test if an object is included in another object.</p></li>
<li><p><code>LocalCache</code> strategy is now a real middleware class and no longer an anonymous class.</p></li>
<li><p><code>ActiveSupport::Dependencies::ClassCache</code> class has been introduced for holding references to reloadable classes.</p></li>
<li><p><code>ActiveSupport::Dependencies::Reference</code> has been refactored to take direct advantage of the new <code>ClassCache</code>.</p></li>
<li><p>Backports <code>Range#cover?</code> as an alias for <code>Range#include?</code> in Ruby 1.8.</p></li>
<li><p>Added <code>weeks_ago</code> and <code>prev_week</code> to Date/DateTime/Time.</p></li>
<li><p>Added <code>before_remove_const</code> callback to <code>ActiveSupport::Dependencies.remove_unloadable_constants!</code>.</p></li>
</ul>
<p>Deprecations:</p>
<ul>
<li>
<code>ActiveSupport::SecureRandom</code> is deprecated in favor of <code>SecureRandom</code> from the Ruby standard library.</li>
</ul>
<h3 id="credits">10 Credits</h3><p>See the <a href="http://contributors.rubyonrails.org/">full list of contributors to Rails</a> for the many people who spent many hours making Rails, the stable and robust framework it is. Kudos to all of them.</p><p>Rails 3.1 Release Notes were compiled by <a href="https://github.com/vijaydev">Vijay Dev</a></p>

        <h3>反馈</h3>
        <p>
          我们鼓励您帮助提高本指南的质量。
        </p>
        <p>
          如果看到如何错字或错误，请反馈给我们。
          您可以阅读我们的<a href="http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">文档贡献</a>指南。
        </p>
        <p>
          您还可能会发现内容不完整或不是最新版本。
          请添加缺失文档到 master 分支。请先确认 <a href="http://edgeguides.rubyonrails.org">Edge Guides</a> 是否已经修复。
          关于用语约定，请查看<a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南指导</a>。
        </p>
        <p>
          无论什么原因，如果你发现了问题但无法修补它，请<a href="https://github.com/rails/rails/issues">创建 issue</a>。
        </p>
        <p>
          最后，欢迎到 <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs 邮件列表</a>参与任何有关 Ruby on Rails 文档的讨论。
        </p>
        <h4>中文翻译反馈</h4>
        <p>贡献：<a href="https://github.com/ruby-china/guides">https://github.com/ruby-china/guides</a>。</p>
      </div>
    </div>
  </div>

  <hr class="hide" />
  <div id="footer">
    <div class="wrapper">
      <p>本著作采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/">创作共用 署名-相同方式共享 4.0 国际</a> 授权</p>
<p>“Rails”，“Ruby on Rails”，以及 Rails Logo 为 David Heinemeier Hansson 的商标。版权所有</p>

    </div>
  </div>

  <script type="text/javascript" src="javascripts/jquery.min.js"></script>
  <script type="text/javascript" src="javascripts/responsive-tables.js"></script>
  <script type="text/javascript" src="javascripts/guides.js"></script>
  <script type="text/javascript" src="javascripts/syntaxhighlighter.js"></script>
  <script type="text/javascript">
    syntaxhighlighterConfig = {
      autoLinks: false,
    };
    $(guidesIndex.bind);
  </script>
</body>
</html>
